extends KinematicBody2D


var speed = 200 # 速度值
var direction = Vector2.ZERO # 方向向量
var velocity = Vector2.ZERO # 速度向量

# 单位时间间隔增加的压力值
var unit_pressure = 0.1

func _physics_process(delta): # 每物理帧
	showPressure()
	velocity = Vector2.ZERO # 速度向量归零
#	direction = Vector2.ZERO # 方向向量归零
	# 四个方向键的压力值随按键松开而迅速归零
	if Input.is_action_just_released("ui_left"):
		direction.x = 0
		$leftTimer.stop()
	if Input.is_action_just_released("ui_right"):
		direction.x = 0
		$rightTimer.stop()
	if Input.is_action_just_released("ui_up"):
		direction.y = 0
		$upTimer.stop()
	if Input.is_action_just_released("ui_down"):
		direction.y = 0
		$downTimer.stop()
	# 四个方向键从按下开始进行随时间进行压力值的增加
	if Input.is_action_pressed("ui_left"):
		$leftTimer.start()
	if Input.is_action_pressed("ui_right"):
		$rightTimer.start()
	if Input.is_action_pressed("ui_up"):
		$upTimer.start()
	if Input.is_action_pressed("ui_down"):
		$downTimer.start()
	# 计算速度向量
	# 速度向量 = 方向向量 * 速度值
	velocity = direction.normalized() * speed
	# 按速度向量进行移动和碰撞检测
	move_and_collide(velocity * delta)

func _on_leftTimer_timeout():
	if direction.x > -1:
		direction.x -= unit_pressure
	pass

func _on_rightTimer_timeout():
	if direction.x < 1:
		direction.x += unit_pressure
	pass

func _on_upTimer_timeout():
	if direction.y > -1:
		direction.y -= unit_pressure
	pass

func _on_downTimer_timeout():
	if direction.y < 1:
		direction.y += unit_pressure
	pass

func showPressure():
	$Label.text = "x压力：%s,y压力:%s" % [direction.x,direction.y]
